package com.bjsxt.rpc.connection;


import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

/**
 * 专门提供Zookeeper连接的自定义类型。
 * */
public class ZkConnection {
    private String zkServer;
    //保存zk的地址，格式是ip:port。如：192.168.140.22:2181

    private int sessionTimeout;
    //保存会话超时时间

    public ZkConnection(){
        super();
        this.zkServer = "localhost:2181";
        //设置默认值

        this.sessionTimeout = 70000;
        //设置超时时间

    }

    public ZkConnection(String zkServer, int sessionTimeout) {
        this.zkServer = zkServer;
        this.sessionTimeout = sessionTimeout;
    }

    /*
    * 创建对象
    * */
    public ZooKeeper getConnection() throws IOException {
        return new ZooKeeper(zkServer, sessionTimeout, new Watcher() {
            public void process(WatchedEvent watchedEvent) {

            }
        });
    }
}
